package com.ynet.middleground.mobile.approve.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ifp.util.core.DateUtil;
import com.ynet.core.codec.mark.util.ObjectUtil;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.mobile.approve.dao.ReceiveOperateMqLogMapper;
import com.ynet.middleground.mobile.approve.entity.ReceiveOperateMqLog;
import com.ynet.middleground.mobile.approve.service.ReceiveOperateMqLogService;

/**
 * ReceiveOperateMqLogServiceImpl
 *
 * @author suremotoo
 * @date 2023/04/03 14:20
 */
@Service
public class ReceiveOperateMqLogServiceImpl extends ServiceImpl<ReceiveOperateMqLogMapper, ReceiveOperateMqLog>
    implements ReceiveOperateMqLogService {

    @Autowired
    private Mapper mapper;

    /**
     * MQ 日志
     *
     * @param msgMap
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void log(Map<String, Object> msgMap) {
        String requestTimeString = ObjectUtil.objectToString(msgMap.get("requestTime"));
        msgMap.remove("requestTime");
        LocalDateTime requestTime;
        try {
            Date date = DateUtil.parse(requestTimeString);
            requestTime = com.ynet.middleground.mobile.approve.utils.DateUtils.asLocalDateTime(date);
        } catch (Exception e) {
            IfpLogger.error("log", "接受渠道消息 requestTime 转换出错！requestTimeString: {}, 采用当前时间。", requestTimeString);
            requestTime = LocalDateTime.now();
        }
        ReceiveOperateMqLog operateMqLog = mapper.map(msgMap, ReceiveOperateMqLog.class);
        operateMqLog.setGmtCreate(LocalDateTime.now());
        operateMqLog.setRequestTime(requestTime);
        save(operateMqLog);
    }

}
